﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;

namespace GTS.Channel.Api {
    /// <summary>
    /// 神奇时代GWC SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class GWC : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "GWC";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.IsHidden = false;

            //接收值
            string result = Request2.GetRequestInputStream();
            info.Mobile = result.GetMatchingValues("<mobile>(.+?)</mobile>", "<mobile>", "</mobile>").FirstOrDefault() ?? "";
            info.ReceiveQuery = result.GetMatchingValues("<content>(.+?)</content>", "<content>", "</content>").FirstOrDefault() ?? "";
            info.MODate = (result.GetMatchingValues("<createDate>(.+?)</createDate>", "<createDate>", "</createDate>").FirstOrDefault() ?? "").ToDateTime();
            info.LongPhone = result.GetMatchingValues("<toicp>(.+?)</toicp>", "<toicp>", "</toicp>").FirstOrDefault() ?? "";
            info.LinkID = result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "";
            info.Telcoid = result.GetMatchingValues("<spid>(.+?)</spid>", "<spid>", "</spid>").FirstOrDefault() ?? "";
            info.CountryCode = result.GetMatchingValues("<mcc>(.+?)</mcc>", "<mcc>", "</mcc>").FirstOrDefault() ?? "";
            info.Price = Convert.ToDecimal(result.GetMatchingValues("<feeprice>(.+?)</feeprice>", "<feeprice>", "</feeprice>").FirstOrDefault() ?? "");
            info.Unit = result.GetMatchingValues("<currency>(.+?)</currency>", "<currency>", "</currency>").FirstOrDefault() ?? "";
            info.PLMN = (result.GetMatchingValues("<mcc>(.+?)</mcc>", "<mcc>", "</mcc>").FirstOrDefault() ?? "") + (result.GetMatchingValues("<mnc>(.+?)</mnc>", "<mnc>", "</mnc>").FirstOrDefault() ?? "");
            info.SMSKey = (result.GetMatchingValues("<istest>(.+?)</istest>", "<istest>", "</istest>").FirstOrDefault() ?? "") == "true" ? 1 : 0;

            if (info.Price <= 0) Base.WriteLog("mo金额错误：" + info.Price);
            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty()) return null;
            if (info.CountryCode == "620" || info.CountryCode == "621" || info.CountryCode == "250") info.ResponseStatus = 1;

            info = Base.GetInfo(info);
            
            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MO（" + DateTime.Now + "）：" + result);
            
            return info;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 3;

            //接收值
            string result = Request2.GetRequestInputStream().ReplaceRN(" ");
            info.SMSContent = result.GetMatchingValues("<content>(.+?)</content>", "<content>", "</content>").FirstOrDefault() ?? "";
            info.LinkID = result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "";
            info.SMSKey = (result.GetMatchingValues("<istest>(.+?)</istest>", "<istest>", "</istest>").FirstOrDefault() ?? "") == "true" ? 1 : 0;
            info.Mobile = result.GetMatchingValues("<mobile>(.+?)</mobile>", "<mobile>", "</mobile>").FirstOrDefault() ?? "";
            info.MRDate = (result.GetMatchingValues("<createDate>(.+?)</createDate>", "<createDate>", "</createDate>").FirstOrDefault() ?? "").ToDateTime();
            info.LongPhone = result.GetMatchingValues("<toicp>(.+?)</toicp>", "<toicp>", "</toicp>").FirstOrDefault() ?? "";
            info.Telcoid = result.GetMatchingValues("<spid>(.+?)</spid>", "<spid>", "</spid>").FirstOrDefault() ?? "";
            info.CountryCode = result.GetMatchingValues("<mcc>(.+?)</mcc>", "<mcc>", "</mcc>").FirstOrDefault() ?? "";
            info.Price = Convert.ToDecimal(result.GetMatchingValues("<feeprice>(.+?)</feeprice>", "<feeprice>", "</feeprice>").FirstOrDefault() ?? "");
            info.Unit = result.GetMatchingValues("<currency>(.+?)</currency>", "<currency>", "</currency>").FirstOrDefault() ?? "";
            if (info.LinkID.IsNullEmpty()) return null;
            string plmn = ChannelDealDataHelper.GetPLMN("ConnString", info.LinkID, SPCode);
            info.PLMN = plmn.IsNullEmpty() ? ((result.GetMatchingValues("<mcc>(.+?)</mcc>", "<mcc>", "</mcc>").FirstOrDefault() ?? "") + (result.GetMatchingValues("<mnc>(.+?)</mnc>", "<mnc>", "</mnc>").FirstOrDefault() ?? "")) : plmn;

            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MT（" + DateTime.Now + "）：" + result);

            return info;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 2;

            //接收值
            string result = Request2.GetRequestInputStream().ReplaceRN(" ");
            info.ResponseStatus = (result.GetMatchingValues("<statestr>(.+?)</statestr>", "<statestr>", "</statestr>").FirstOrDefault() ?? "") == "DELIVRD" ? 1 : 0;
            info.MRDate = (result.GetMatchingValues("<createDate>(.+?)</createDate>", "<createDate>", "</createDate>").FirstOrDefault() ?? "").ToDateTime();
            info.LinkID = result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "";
            info.SMSKey = (result.GetMatchingValues("<istest>(.+?)</istest>", "<istest>", "</istest>").FirstOrDefault() ?? "") == "true" ? 1 : 0;

            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MR（" + DateTime.Now + "）：" + result);

            return info;
        }
        /// <summary>
        /// IVR的参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            return null;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res == ResultEnum.Success ? "status=0" : "status=1");
        }
    }
}

